#!/bin/bash

# Exit on any error
set -e

# Configuration
REMOTE_HOST="42.51.37.194"
REMOTE_USER="root"
REMOTE_DIR="/root/location_tracker_admin"
GITHUB_REPO="https://gitee.com/xusidev/location_tracker_admin.git"

# Colors for output
GREEN='\033[0;32m'
NC='\033[0m'

echo -e "${GREEN}Starting deployment process...${NC}"

# Commit and push changes
echo -e "${GREEN}Committing and pushing changes...${NC}"
git add .
git commit -m "Deployment update $(date)"
git push origin main

# Connect to remote server and deploy
echo -e "${GREEN}Deploying to remote server...${NC}"
ssh $REMOTE_USER@$REMOTE_HOST << 'ENDSSH'
    # Navigate to deployment directory
    cd $REMOTE_DIR

    # Pull latest changes
    git pull origin main

    # Build and start Docker containers
    docker-compose down
    docker-compose build --no-cache
    docker-compose up -d

    # Check if containers are running
    docker-compose ps

    # Show logs
    docker-compose logs -f
ENDSSH

echo -e "${GREEN}Deployment completed successfully!${NC}"